<?php
namespace Admin\Controller;
use Think\Controller;
use Admin\Logic\UpgradeLogic;
use PHPExcel_Cell;
use PHPExcel_IOFactory;
use Think\Exception;
class BaseController extends Controller{
	public $fenbu_list=array();
	
    /**
     * 析构函数
     */
    function __construct() 
    {
   
        parent::__construct();
        $upgradeLogic = new UpgradeLogic();
        $upgradeMsg = $upgradeLogic->checkVersion(); //升级包消息
        $this->assign('upgradeMsg',$upgradeMsg);
        //用户中心面包屑导航
        $navigate_admin = navigate_admin();
        $this->assign('navigate_admin',$navigate_admin);
        tpversion();
        $shop_info=tpCache("shop_info");
        $this->assign('shop_info',$shop_info);
   }    
    
    /*
     * 初始化操作
     */
    public function _initialize() 
    {
        $this->assign('action',ACTION_NAME);
        //过滤不需要登陆的行为
        if(in_array(ACTION_NAME,array('login','logout','vertify','pda_qr_show','pda_qr_login','mysafety','export_order_settled')) || in_array(CONTROLLER_NAME,array('Ueditor','Uploadify','Import'))){
          
        	//return;
        }else{
            
        	if(session('admin_id') > 0 ){
        		$this->check_priv();//检查管理员菜单操作权限
        	}else{
        		$this->error('请先登陆',U('Admin/Admin/login'),1);
        	}
        }
		$this->fenbu_list=M('fenbu')->order('fenbu_sort asc')->select();

        $this->public_assign();
    }
    
    /**
     * 保存公告变量到 smarty中 比如 导航 
     */
    public function public_assign()
    {
       $tpshop_config = array();
       $tp_config = M('config')->select();
       foreach($tp_config as $k => $v)
       {
          $tpshop_config[$v['inc_type'].'_'.$v['name']] = $v['value'];
       }
       $this->assign('tpshop_config', $tpshop_config);
    }
    
    public function check_priv()
    {
    	$ctl = CONTROLLER_NAME;
    	$act = ACTION_NAME;
		$act_list = session('act_list');
        // dump($act_list);
		//无需验证的操作
		$uneed_check = array('login','logout','vertifyHandle','vertify','imageUp','upload','login_task','mysafety');
    	if($ctl == 'Index' || $act_list == 'all'){
    		//后台首页控制器无需验证,超级管理员无需验证
    		return true;
    	}elseif(strpos('ajax',$act) || in_array($act,$uneed_check)){
    		//所有ajax请求不需要验证权限
    		return true;
    	}else{
    		$right = M('system_menu')->where("id in($act_list)")->getField('right',true);
    // 		dump($right);
    		foreach ($right as $val){
    			$role_right .= $val.',';
    		}
    		$role_right = explode(',', $role_right);
    		//检查是否拥有此操作权限
    		if(!in_array($ctl.'Controller@'.$act, $role_right)){
    			$this->error('您没有操作权限,请联系超级管理员分配权限',U('Admin/Index/welcome'));
    		}
    	}
    }
    /**
     * 读取 excel 文件内容
     * @param string    $excelFile 文件路径
     * @param int       $startRow 开始行
     * @param null      $endRow 结束行
     * @return array
     * @throws PHPExcel_Reader_Exception
     */
    public function readFromExcel($excelFile, $startRow = 2, $endRow = null)
    {
        import("Org.Util.PHPExcel");
        import("Org.Util.PHPExcel.Worksheet.Drawing");
        import("Org.Util.PHPExcel.Writer.Excel2007");

        $inputFileType = PHPExcel_IOFactory::identify($excelFile);

        try {
            $excelReader = PHPExcel_IOFactory::createReader($inputFileType);
            $phpexcel    = $excelReader->load($excelFile);
            $activeSheet = $phpexcel->getActiveSheet();
            if (!$endRow) {
                $endRow = $activeSheet->getHighestRow(); //总行数
            }

            $highestColumn      = $activeSheet->getHighestColumn(); //最后列数所对应的字母，例如第2行就是B
            $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); //总列数

            $data = array();

            for ($row = $startRow; $row <= $endRow; $row++) {
                for ($col = 0; $col < $highestColumnIndex; $col++) {
                    $data[$row][] = (string) $activeSheet->getCellByColumnAndRow($col, $row)->getCalculatedValue();
                }
            }
        } catch (Exception $e) {
            $arr['code'] = 1001;
            $arr['msg'] = $e->getMessage();
            return $arr;
        }

        $arr['code'] = '1000';
        $arr['data'] = $data;
        return $arr;
    }
}